// content-scripts/services/screenshot-service.js
import { html2CanvasLoader } from '../../utils/html2canvas-loader.js';

export class ScreenshotService {
    static defaultConfig = {
        backgroundColor: '#FFFFFF',
        scale: 2,
        logging: false,
        useCORS: true,
        removeContainer: true
    };

    static async capture(element, customConfig = {}) {
        try {
            // 确保库已加载
            await html2CanvasLoader.load();

            // 合并配置
            const config = {
                ...this.defaultConfig,
                ...customConfig,
                scale: window.devicePixelRatio > 1 ? 2 : 1
            };

            // 执行截图
            const canvas = await window.html2canvas(element, config);

            // 自动清理临时容器
            if (config.removeContainer && element.dataset.tempContainer) {
                element.remove();
            }

            return canvas;
        } catch (error) {
            console.error('截图失败:', error);
            throw new Error('CAPTURE_FAILED');
        }
    }
}
